package net.oschina.simpleplus.erpc.support;

import com.google.protobuf.GeneratedMessageV3;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class ERpcMessageEncoder extends MessageToByteEncoder<Protobuf<GeneratedMessageV3>>{

	@Override
	protected void encode(ChannelHandlerContext ctx,Protobuf<GeneratedMessageV3> msg, ByteBuf out) throws Exception {
		byte[] bs = msg.toByteArray();
		ByteBuf buf = Unpooled.buffer(bs.length + 4);
		buf.writeInt(bs.length);
		buf.writeBytes(bs);
		out.writeBytes(buf);
	}

}
